#!/usr/bin/env bash
this_dir=$(cd $(dirname $0) && pwd)
#source $this_dir/config.sh

projet_name=fjp
build=0
debug=0
server=
#-------read the options----------------
opts=$(getopt -o bdp:: --long server:: -- "$@")
eval set -- "$opts"
while true; do
    case "$1" in
        -h|-\?)
            echo "-b build"
            echo "-d debug publis"
            echo "-p publish to server"
            exit 0
            ;;

        -d) 
            debug=1; shift ;;

        -b) 
            build=1; shift ;;

        -p|--server)
             case "$2" in 
                 * ) server=$2 ;;
             esac
             shift 2;;

        --) shift; break;;
        *) echo "error"; exit ;;
    esac
done
#echo $build,$server
#------------------------------------------


if [ $build -eq 1 ]; then
    egret build
    egret publish
fi

dirname=`command ls -rt release/html5|tail -1`
mkdir -p ~/www/h5/fjp/latest
cp -Rf release/html5/$dirname/* ~/www/h5/fjp/latest/1/

if [ -n "$server" ]; then
    dirname=`command ls -rt release/html5|tail -1`
    echo "pub [$dirname] to [$server] debug[$debug]"
    if [ $debug -eq 1 ]; then
        rsync -vrP --checksum --delete release/html5/$dirname/ wellbye@$server:www/h5/$projet_name/debug/
    else
        rsync -vrP --checksum --delete release/html5/$dirname/ wellbye@$server:www/h5/$projet_name/latest/
    fi
fi

